package com.lxyk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lxyk.yygh.hosp.repository.DepartmentRepository;
import com.lxyk.yygh.hosp.service.DepartmentService;
import com.lxyk.yygh.model.hosp.Department;
import com.lxyk.yygh.vo.hosp.DepartmentQueryVo;
import com.lxyk.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void save(Map<String, Object> paramMap) {
        //把paramMap  装换为  Department
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString, Department.class);

        //根据科室编号和医院编号查询     getDepartmentByHoscodeAndDepcode取名字一定要规范
        Department tarDepartment = departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        //判断 是否存在，存在就更新，不存在就新增
        if (null != tarDepartment){
            tarDepartment.setUpdateTime(new Date());
            tarDepartment.setIsDeleted(0);
            departmentRepository.save(tarDepartment);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(null!=department)
            //调用删除方法
            departmentRepository.deleteById(department.getId());
    }

    //根据编号查询所有科室
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //最终返回的集合
        List<DepartmentVo> resultList=new ArrayList<>();

        //1.根据编号拿到所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        //因为数据在Mongodb里面，所以用Example
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);//拿到所有数据

        //2.根据大科室编号 分组,获取每个大科室下面的子科室        Collectors.groupingBy()根据大科室编号分组
        Map<String, List<Department>> departmentMap = departmentList.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));
        /**分组成一组一组的形式
         * 内科
         * 外科
         * ...
         */
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {//departmentMap.entrySet()遍历Map集合
            //大科室编号
            String key = entry.getKey();
            //大科室编号对应的数据
            List<Department> listValue = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(listValue.get(0).getDepname());
            //封装小科室
            List<DepartmentVo> departList=new ArrayList<>();
            for (Department depart : listValue) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(depart.getDepcode());
                departmentVo2.setDepname(depart.getDepname());
                departList.add(departmentVo2);
            }
            //把小科室放到大科室里面
            departmentVo.setChildren(departList);
            //放到最终的集合里面去 返回
            resultList.add(departmentVo);
        }
        return resultList;
    }

    //科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(null!=department){
            return department.getDepname();
        }
        return "";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }

    //查询科室带分页
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //Mongodb第一页是0 ，所以page-1
        PageRequest pageable = PageRequest.of(page - 1, limit);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        //匹配器
        ExampleMatcher matcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //使用字符串匹配
                .withIgnoreCase(true);//忽略大小写

        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }
}
